import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_wan_android/page/login.dart';
import 'package:flutter_wan_android/routes/routes.dart';

import '../application.dart';

class RouteUtil {

  /// push
  static push(
    BuildContext context,
    String path, {
    Map? params,
    bool replace = false,
    bool checkLogin = false
  }) {

    // 需要校验是否登录的页面，没登录需要跳转到登录页面
    if(checkLogin && !Application.hasLogin){
      toLoginPage();
      return;
    }

    Application.router.navigateTo(context, path,
        replace: replace, routeSettings: RouteSettings(arguments: params));
  }

  /// pop
  static pop(BuildContext context) => Application.router.pop(context);

  /// 获取路由参数
  static Map<dynamic, dynamic> getParams(BuildContext context) {
    var result = ModalRoute.of(context)?.settings.arguments;
    if (result == null) return {};
    return result as Map<dynamic, dynamic>;
  }

  /// 去登录页，用于token失效等，全局无context时的跳转
  static toLoginPage() {
    Application.navGK.currentState?.push(Right2LeftRouter(child: Login())
        // MaterialPageRoute(
        //   builder: (context) => Login(),
        // ),
        );
  }
}

///
//右--->左
class Right2LeftRouter<T> extends PageRouteBuilder<T> {
  Widget? child;
  int duration_ms;
  Curve curve;

  Right2LeftRouter(
      {this.child, this.duration_ms = 250, this.curve = Curves.fastOutSlowIn})
      : super(
            transitionDuration: Duration(milliseconds: duration_ms),
            pageBuilder: (ctx, a1, a2) {
              return child!;
            },
            transitionsBuilder: (
              ctx,
              a1,
              a2,
              Widget child,
            ) {
              return SlideTransition(
                  position: Tween<Offset>(
                    begin: Offset(1.0, 0.0),
                    end: Offset(0.0, 0.0),
                  ).animate(
                    CurvedAnimation(parent: a1, curve: curve),
                  ),
                  child: child);
            });
}
